import simplejson as json

class SimpleWriter():
	def __init__(self, response):
		self.response = response

	def write(self, suite_result):
		self.response.out.write("Total Test Count: " + str(suite_result.total_test_count()) + "<br/>")
		self.response.out.write("Passing Test Count: " + str(suite_result.passing_test_count()) + "<br/>")
		self.response.out.write("Failing Test Count: " + str(suite_result.failing_test_count()) + "<br/>")
		self.response.out.write("Ignored Test Count: " + str(suite_result.ignored_test_count()) + "<br/>")
		self.response.out.write("Total Run Time: " + str(suite_result.total_run_time) + "<br/>")
		
		for module_result in suite_result.module_results:
			SimpleWriter(self.response).print_module_result(module_result)
		
	def print_module_result(self, module_result):
		self.response.out.write("<br />MODULE: " + module_result.module_name)
		for fixture_result in module_result.fixture_results:
			self.print_fixture_result(fixture_result)
		
	def print_fixture_result(self, fixture_result):
		self.response.out.write("<br />FIXTURE:" + fixture_result.fixture_name + "<br />")
		for result in fixture_result.test_results:
			if (result.is_ignored):
				self.write_with_color(result, "orange")
			else:
				if (result.is_successful):
					self.write_with_color(result, "green")
				else:
					self.write_with_color(result, "red")
					self.response.out.write("<br />")
					self.write_with_color(result.stack_trace.replace("\n", "<br/>"), "blue")
			self.response.out.write("<br />")
						
	def write_with_color(self, obj, color):
		self.response.out.write('<span style="color:%s;">%s</span>' % (color, str(obj)))


class JSONWriter():
	def __init__(self, response):
		self.response = response
		
	def write(self, suite_result):
		suite_result_json = suite_result.to_json()
		json_dictionary = {"suite_result" : suite_result_json}
		self.response.out.write(json.dumps(json_dictionary))